home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / MooseX / Traits.pm < prev    next >
Encoding:
Perl POD Document  |  2010-05-13  |  5.5 KB  |  231 lines

  1. package MooseX::Traits;
  2. use Moose::Role;
  3.  
  4. use MooseX::Traits::Util qw(new_class_with_traits);
  5.  
  6. use warnings;
  7. use warnings::register;
  8.  
  9. use namespace::autoclean;
  10.  
  11. our $VERSION   = '0.11';
  12. our $AUTHORITY = 'id:JROCKWAY';
  13.  
  14. has '_trait_namespace' => (
  15.     # no accessors or init_arg
  16.     init_arg => undef,
  17.     isa      => 'Str',
  18.     is       => 'bare',
  19. );
  20.  
  21. sub with_traits {
  22.     my ($class, @traits) = @_;
  23.  
  24.     my $new_class = new_class_with_traits(
  25.         $class,
  26.         @traits,
  27.     );
  28.  
  29.     return $new_class->name;
  30. }
  31.  
  32. # somewhat deprecated, but use if you want to
  33. sub new_with_traits {
  34.     my $class = shift;
  35.  
  36.     my ($hashref, %args) = 0;
  37.     if (ref($_[0]) eq 'HASH') {
  38.         %args    = %{ +shift };
  39.         $hashref = 1;
  40.     } else {
  41.         %args    = @_;
  42.     }
  43.  
  44.     my $traits = delete $args{traits} || [];
  45.  
  46.     my $new_class = $class->with_traits(ref $traits ? @$traits : $traits );
  47.  
  48.     my $constructor = $new_class->meta->constructor_name;
  49.     confess "$class ($new_class) does not have a constructor defined via the MOP?"
  50.       if !$constructor;
  51.  
  52.     return $new_class->$constructor($hashref ? \%args : %args);
  53.  
  54. }
  55.  
  56. # this code is broken and should never have been added.  i probably
  57. # won't delete it, but it is definitely not up-to-date with respect to
  58. # other features, and never will be.
  59. #
  60. # runtime role application is fundamentally broken.  if you really
  61. # need it, write it yourself, but consider applying the roles before
  62. # you create an instance.
  63.  
  64. sub apply_traits {
  65.     my ($self, $traits, $rebless_params) = @_;
  66.  
  67.     # disable this warning with "use MooseX::Traits; no warnings 'MooseX::Traits'"
  68.     warnings::warnif('apply_traits is deprecated due to being fundamentally broken. '.
  69.                      q{disable this warning with "no warnings 'MooseX::Traits'"});
  70.  
  71.     # arrayify
  72.     my @traits = $traits;
  73.     @traits = @$traits if ref $traits;
  74.  
  75.     if (@traits) {
  76.         @traits = MooseX::Traits::Util::resolve_traits(
  77.             $self, @traits,
  78.         );
  79.  
  80.         for my $trait (@traits){
  81.             $trait->meta->apply($self, rebless_params => $rebless_params || {});
  82.         }
  83.     }
  84. }
  85.  
  86. no Moose::Role;
  87.  
  88. 1;
  89.  
  90. __END__
  91.  
  92. =head1 NAME
  93.  
  94. MooseX::Traits - automatically apply roles at object creation time
  95.  
  96. =head1 SYNOPSIS
  97.  
  98. Given some roles:
  99.  
  100.   package Role;
  101.   use Moose::Role;
  102.   has foo => ( is => 'ro', isa => 'Int' required => 1 );
  103.  
  104. And a class:
  105.  
  106.   package Class;
  107.   use Moose;
  108.   with 'MooseX::Traits';
  109.  
  110. Apply the roles to the class at C<new> time:
  111.  
  112.   my $class = Class->with_traits('Role')->new( foo => 42 );
  113.  
  114. Then use your customized class:
  115.  
  116.   $class->isa('Class'); # true
  117.   $class->does('Role'); # true
  118.   $class->foo; # 42
  119.  
  120. =head1 DESCRIPTION
  121.  
  122. Often you want to create components that can be added to a class
  123. arbitrarily.  This module makes it easy for the end user to use these
  124. components.  Instead of requiring the user to create a named class
  125. with the desired roles applied, or apply roles to the instance
  126. one-by-one, he can just create a new class from yours with
  127. C<with_traits>, and then instantiate that.
  128.  
  129. There is also C<new_with_traits>, which exists for compatibility
  130. reasons.  It accepts a C<traits> parameter, creates a new class with
  131. those traits, and then insantiates it.
  132.  
  133.    Class->new_with_traits( traits => [qw/Foo Bar/], foo => 42, bar => 1 )
  134.  
  135. returns exactly the same object as
  136.  
  137.    Class->with_traits(qw/Foo Bar/)->new( foo => 42, bar => 1 )
  138.  
  139. would.  But you can also store the result of C<with_traits>, and call
  140. other methods:
  141.  
  142.    my $c = Class->with_traits(qw/Foo Bar/);
  143.    $c->new( foo => 42 );
  144.    $c->whatever( foo => 1234 );
  145.  
  146. And so on.
  147.  
  148. =head1 METHODS
  149.  
  150. =over 4
  151.  
  152. =item B<< $class->with_traits( @traits ) >>
  153.  
  154. Return a new class with the traits applied.  Use like:
  155.  
  156. =item B<< $class->new_with_traits(%args, traits => \@traits) >>
  157.  
  158. C<new_with_traits> can also take a hashref, e.g.:
  159.  
  160.   my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
  161.  
  162. =back
  163.  
  164. =head1 ATTRIBUTES YOUR CLASS GETS
  165.  
  166. This role will add the following attributes to the consuming class.
  167.  
  168. =head2 _trait_namespace
  169.  
  170. You can override the value of this attribute with C<default> to
  171. automatically prepend a namespace to the supplied traits.  (This can
  172. be overridden by prefixing the trait name with C<+>.)
  173.  
  174. Example:
  175.  
  176.   package Another::Trait;
  177.   use Moose::Role;
  178.   has 'bar' => (
  179.       is       => 'ro',
  180.       isa      => 'Str',
  181.       required => 1,
  182.   );
  183.  
  184.   package Another::Class;
  185.   use Moose;
  186.   with 'MooseX::Traits';
  187.   has '+_trait_namespace' => ( default => 'Another' );
  188.  
  189.   my $instance = Another::Class->new_with_traits(
  190.       traits => ['Trait'], # "Another::Trait", not "Trait"
  191.       bar    => 'bar',
  192.   );
  193.   $instance->does('Trait')          # false
  194.   $instance->does('Another::Trait') # true
  195.  
  196.   my $instance2 = Another::Class->new_with_traits(
  197.       traits => ['+Trait'], # "Trait", not "Another::Trait"
  198.   );
  199.   $instance2->does('Trait')          # true
  200.   $instance2->does('Another::Trait') # false
  201.  
  202. =head1 AUTHORS and CONTRIBUTORS
  203.  
  204. Jonathan Rockway C<< <jrockway@cpan.org> >>
  205.  
  206. Stevan Little C<< <stevan.little@iinteractive.com> >>
  207.  
  208. Tomas Doran C<< <bobtfish@bobtfish.net> >>
  209.  
  210. Matt S. Trout C<< <mst@shadowcatsystems.co.uk> >>
  211.  
  212. Jesse Luehrs C<< <doy at tozt dot net> >>
  213.  
  214. Shawn Moore C<< <sartak@bestpractical.com> >>
  215.  
  216. Florian Ragwitz C<< <rafl@debian.org> >>
  217.  
  218. Chris Prather C<< <chris@prather.org> >>
  219.  
  220. Yuval Kogman C<< <nothingmuch@woobling.org> >>
  221.  
  222. =head1 COPYRIGHT AND LICENSE
  223.  
  224. Copyright 2008 Infinity Interactive, Inc.
  225.  
  226. L<http://www.iinteractive.com>
  227.  
  228. This library is free software; you can redistribute it and/or modify
  229. it under the same terms as Perl itself.
  230.  
  231.